package 接口测试;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import org.apache.hc.core5.http.ClassicHttpResponse;


import javax.net.ssl.*;

import java.security.cert.X509Certificate;

public class DoctorClient {

    private static final String SERVICE_URL = "https://192.168.253.15:1443/imedical/webservice/CHSSWeb.ChuZhou.SignService.cls";

    public static void main(String[] args) {
        try (CloseableHttpClient client = createInsecureHttpClient()) {
            HttpPost post = new HttpPost(SERVICE_URL);

            // 设置请求头
            post.setHeader("Content-Type", "text/xml; charset=utf-8");
            post.setHeader("SOAPAction", "\"http://tempuri.org/getdoctorlist\"");

            // 构建带有认证信息的 SOAP 请求体
            String soapRequest = buildSoapRequest();
            StringEntity entity = new StringEntity(soapRequest, "UTF-8");
            post.setEntity(entity);

            // 发送请求并获取响应
            ClassicHttpResponse response = (ClassicHttpResponse) client.execute(post);
            System.out.println("Status Code: " + response.getCode());

            if (response.getEntity() != null) {
                String responseBody = EntityUtils.toString((HttpEntity) response.getEntity(), "UTF-8");
                System.out.println("Response Body:\n" + responseBody);
            }

        } catch (Exception e) {
            System.err.println("Error occurred while sending request:");
            e.printStackTrace();
        }
    }

    /**
     * 构建包含 UsernameToken 的 SOAP 请求
     */
    private static String buildSoapRequest() {
        return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
                "               xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">\n" +
                "  <soap:Header>\n" +
                "    <wsse:Security>\n" +
                "      <wsse:UsernameToken>\n" +
                "        <wsse:Username>dhact</wsse:Username>\n" +
                "        <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">dhact</wsse:Password>\n" +
                "      </wsse:UsernameToken>\n" +
                "    </wsse:Security>\n" +
                "  </soap:Header>\n" +
                "  <soap:Body>\n" +
                "    <s0:GetCommunityInfo xmlns:s0=\"http://tempuri.org\">\n" +
                "      <s0:strJSON>{\"TeamRowId\": \"23\"}</s0:strJSON>\n" +
                "    </s0:GetCommunityInfo>\n" +
                "  </soap:Body>\n" +
                "</soap:Envelope>";
    }

    /**
     * 创建忽略 SSL 验证的 HttpClient（仅用于测试）
     */
    private static CloseableHttpClient createInsecureHttpClient() throws NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = SSLContext.getInstance("TLS");

        sslContext.init(null, new TrustManager[]{new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) {}
            public void checkServerTrusted(X509Certificate[] chain, String authType) {}
            public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
        }}, null);

        return HttpClients.custom()
                .setSSLContext(sslContext)
                .build();
    }
}